package com.example.myapplication.broadcast;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.example.myapplication.MainActivity;
import com.example.myapplication.fragment.FFActivity;

public class MyBroadcastReceiver extends BroadcastReceiver {

    private static final String ACTION_BOOT = "com.example.myapplication.MY_BROADCAST";


    @SuppressLint("LongLogTag")
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(ACTION_BOOT) || intent.getAction().equals("GUANGBO")){
            Log.i(ACTION_BOOT, "onStartCommand方法被调用!");
            // 模拟使用广播传递消息
            String name = intent.getStringExtra("name");
            String pwd = intent.getStringExtra("pwd");
            if(name.equals("123") && pwd.equals("123")){
                Toast.makeText(context, "收到信息啦~"+ "name =" + name + ",pwd =" + pwd + "跳转页面",Toast.LENGTH_SHORT).show();
            }else {
                control(context,intent);
            }
        }
        // 系统电量
        if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
            int level = intent.getIntExtra("level", 0);
            Toast.makeText(context, "电池电量为" + level + "%", Toast.LENGTH_SHORT).show();
            if (level < 15) {
                Toast.makeText(context, "电池电量不足15%，请及时充电", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 广播逻辑处理
     * @param context
     * @param intent
     */
    public void control(Context context,Intent intent){
        //创建一个警告对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle("重要提醒");
        builder.setMessage("检测到您的账号在别处登录，强制下线，如不是本人操作，请修改密码！");

        AlertDialog.Builder builder1 = builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 页面跳转
                intent.setClassName("com.example.myapplication", "com.example.myapplication.MainActivity");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(context, MainActivity.class);
                context.startActivity(intent);
                Toast.makeText(context,"选择了确定",Toast.LENGTH_SHORT).show();
            }

        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context,"选择了取消",Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNeutralButton("再想想", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context,"选择了再想想",Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog =builder.create();//这个方法可以返回一个alertDialog对象
        alertDialog.show();
    }
}